package org.iot.iotserver.http.config;

import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.annotations.AnnotationParser;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.WebAppContext;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

//import org.eclipse.jetty.annotations.ClassNameResolver;

/**
 * 自定义注解扫描配置
 */
public class MyAnnotationConfiguration extends AnnotationConfiguration {

    private final Resource classPathResources;

    public MyAnnotationConfiguration() throws IOException {
        classPathResources = Resource.newResource("./");
    }

    /**
     * 打包 classes 没有按照 war 包方式打包, 需要重载此方法才能支持 servlet3.0 注解
     * @param context
     * @param parser
     * @throws Exception
     */
    @Override
    public void parseWebInfClasses(WebAppContext context, AnnotationParser parser) throws Exception {
        Set<AnnotationParser.Handler> handlers = new HashSet<AnnotationParser.Handler>();
        handlers.addAll(_discoverableAnnotationHandlers);
        if (_classInheritanceHandler != null)
            handlers.add(_classInheritanceHandler);
        handlers.addAll(_containerInitializerAnnotationHandlers);

        if (_parserTasks != null) {
            ParserTask task = new ParserTask(parser, handlers, classPathResources);
            _parserTasks.add(task);
        }

        super.parseWebInfClasses(context, parser);
    }
}
